// 标题设置功能
document.querySelectorAll('.heading-btn').forEach(btn => {
    btn.addEventListener('click', () => {
        const level = btn.dataset.level;
        document.execCommand('formatBlock', false, `<h${level}>`);
    });
});

// 图片处理功能
document.querySelector('.image-btn').addEventListener('click', () => {
    const input = document.createElement('input');
    input.type = 'file';
    input.accept = 'image/*';
    input.click();
    
    input.onchange = () => {
        const file = input.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                const img = document.createElement('img');
                img.src = e.target.result;
                document.querySelector('.editor').appendChild(img);
            };
            reader.readAsDataURL(file);
        }
    };
});

// 剪贴板图片粘贴
document.querySelector('.editor').addEventListener('paste', (e) => {
    const items = e.clipboardData.items;
    for (let i = 0; i < items.length; i++) {
        if (items[i].type.indexOf('image') !== -1) {
            const blob = items[i].getAsFile();
            const reader = new FileReader();
            reader.onload = (event) => {
                const img = document.createElement('img');
                img.src = event.target.result;
                document.querySelector('.editor').appendChild(img);
            };
            reader.readAsDataURL(blob);
        }
    }
});

// 视频拖放上传
document.querySelector('.editor').addEventListener('dragover', (e) => {
    e.preventDefault();
});

document.querySelector('.editor').addEventListener('drop', (e) => {
    e.preventDefault();
    const file = e.dataTransfer.files[0];
    if (file && file.type.includes('video')) {
        const video = document.createElement('video');
        video.controls = true;
        const reader = new FileReader();
        reader.onload = (event) => {
            video.src = event.target.result;
            document.querySelector('.editor').appendChild(video);
        };
        reader.readAsDataURL(file);
    }
});

// 视频框选功能
document.querySelector('.video-select-btn').addEventListener('click', () => {
    const selection = window.getSelection();
    if (selection.toString().length > 0) {
        const range = selection.getRangeAt(0);
        const selectedContent = range.cloneContents();
        // 这里可以添加视频框选后的处理逻辑
    }
});